package erp.education.server.beans.implementation.setup.school;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import erp.education.client.setup.school.BranchRequest;
import erp.education.server.beans.implementation.BaseService;
import erp.education.server.beans.implementation.ResultResponse;
import erp.education.server.beans.implementation.ServiceObject;
import erp.education.server.beans.interfaces.setup.school.BranchsServiceI;
import erp.education.server.beans.interfaces.user.transaction.ServiceObjectI;
import erp.education.server.db.entites.setup.school.Branchs;
import erp.education.server.db.exceptions.DbExceptions;

@Component("branchsService")
public class BranchsService extends BaseService implements BranchsServiceI {

	@Override
	public ServiceObjectI branchsLoadSevice(BranchRequest branchRequest)
			throws DbExceptions {
		ServiceObjectI response = new ServiceObject();
		ResultResponse holder = new ResultResponse();
		List result = new ArrayList();
		result.add(schoolDao.getSchools());
		result.add(branchsDao.getBranchList());
		holder.setResultList(result);
		response.setResponseObject(holder);
		return response;
	}

	@Override
	public ServiceObjectI saveUpdateBranchDetails(BranchRequest branchRequest)
			throws DbExceptions {
		ServiceObjectI response = new ServiceObject();
		ResultResponse holder = new ResultResponse();
		List result = new ArrayList();
		Branchs branch = null;
		try {
			branch = branchsDao.getBranch(branchRequest.getSchoolId(),
					branchRequest.getBranchName());
		} catch (Exception e) {
		}
		if (null == branch) {
			branch = new Branchs();
			branch.setSchoolId(branchRequest.getSchoolId());
			branch.setBranchName(branchRequest.getBranchName());
		}
		branch.setAddress(branchRequest.getAddress());
		branch.setPhoneNo(branchRequest.getPhoneNo());
		branch.setFaxNo(branchRequest.getFaxNo());
		branch.setEmailId(branchRequest.getEmailId());
		branchsDao.merge(branch);

		holder.setResultList(result);
		response.setResponseObject(holder);
		return response;
	}
}
